package com.wodsy.phoenix.datadao.shopc.dao.impl;

import com.wodsy.phoenix.datadao.base.BaseHibernateDao;
import com.wodsy.phoenix.datadao.shopc.dao.FreightTempDao;
import com.wodsy.phoenix.dataobject.base.Pager;
import com.wodsy.phoenix.dataobject.shopc.model.FreightTemp;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by Huang on 2015/4/15.
 */
@Repository("freightTempDao")
public class FreightTempDaoImpl extends BaseHibernateDao<FreightTemp,Integer> implements FreightTempDao {

    @Autowired
    @Qualifier(value="shopc")
    @Override
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public Class<FreightTemp> getModelClass() {
        return FreightTemp.class;
    }

    @Override
    public Pager<FreightTemp> findFreightTempsByOwnerInfo(Pager<FreightTemp> pager,int shopId, int userId) {

        Criteria criteria = getSession().createCriteria(getModelClass());
        criteria.add(Restrictions.and(Restrictions.eq("shopId",shopId),(Restrictions.eq("userId",userId))));
        criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        criteria.addOrder(Order.desc("updateTime"));
        toPager(pager, criteria);
        return pager;
    }

    @Override
    public List<FreightTemp> findFreightTempsByOwnerInfo(int shopId, int userId) {
        Criteria criteria = getSession().createCriteria(getModelClass());
        criteria.add(Restrictions.and(Restrictions.eq("shopId",shopId),(Restrictions.eq("userId",userId))));
        criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        criteria.addOrder(Order.desc("updateTime"));
        return criteria.list();
    }
}
